package com.example.wjy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.room.entity.TeamMember;
import com.example.superyi.myteam.R;
import com.example.room.entity.Master;

import java.util.List;

/**
 * author: WangJunYan
 * package: com.example.wjy.adapter
 * date: 2020/6/7
 * time: 16:32
 * 在RecyclerView中显示大神的适配器，在MasterFragment中使用
 */

public class MembersAdapter extends RecyclerView.Adapter<MembersAdapter.MembersViewHolder> {

    /**
     * List of masters.
     */
    List<TeamMember> masterList;

    int []colors = {0x80FF4500,0x80FFB90F,0x8098F5FF,0x80836FFF,0x8066CD00,0x80B452CD};

    /**
     * Constructor.
     *
     * @param masterList The list of masters from database
     */
    public MembersAdapter(List<TeamMember> masterList) {
        this.masterList = masterList;
    }

    public void setMasterList(List<TeamMember> masterList) {
        this.masterList = masterList;
    }

    @NonNull
    @Override
    public MembersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        //为RecyclerView中的每一个item设置布局
        View view = inflater.inflate(R.layout.user_item_layout, parent, false);
        return new MembersViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MembersViewHolder holder, int position) {
        //设置每一个item的显示数据，目前只有这两项
//        holder.name.setText(masterList.get(position).getMasterName());
//        holder.type.setText(masterList.get(position).getMasterType());
        holder.userId.setText(masterList.get(position).getUserId());
        holder.userName.setText(masterList.get(position).getUserName());
        holder.userAbility.setText(String.valueOf(masterList.get(position).getUserAbility()));
        holder.userGrade.setText(masterList.get(position).getUserGrade());
        holder.userSpy.setText(masterList.get(position).getUserSpy());
        holder.userSex.setText(masterList.get(position).getUserSex());
        holder.userLabel1.setText(masterList.get(position).getUserLabel1());
        holder.userLabel2.setText(masterList.get(position).getUserLabel2());
        holder.userLabel3.setText(masterList.get(position).getUserLabel3());
        holder.userLabel4.setText(masterList.get(position).getUserLabel4());
        holder.cv.setBackgroundColor(colors[position % 6]);
    }

    @Override
    public int getItemCount() {
//        return masterList.size();
        return masterList == null ? 0 : masterList.size();
    }

    /**
     * 内部类，用于管理每个item的信息。
     */
    static class MembersViewHolder extends RecyclerView.ViewHolder {

        TextView userName;

        TextView userAbility;

        TextView userId;

        TextView userSex;

        TextView userSpy;

        TextView userGrade;

        TextView userLabel1;

        TextView userLabel2;

        TextView userLabel3;

        TextView userLabel4;

        CardView cv;
        public MembersViewHolder(@NonNull View itemView) {
            super(itemView);
            userName = itemView.findViewById(R.id.user_name);
            userAbility = itemView.findViewById(R.id.user_ability);
            userId = itemView.findViewById(R.id.user_id);
            userSpy = itemView.findViewById(R.id.user_spy);
            userGrade = itemView.findViewById(R.id.user_grade);
            userSex = itemView.findViewById(R.id.user_sex);
            userLabel1 = itemView.findViewById(R.id.user_label1);
            userLabel2 = itemView.findViewById(R.id.user_label2);
            userLabel3 = itemView.findViewById(R.id.user_label3);
            userLabel4 = itemView.findViewById(R.id.user_label4);
            cv = itemView.findViewById(R.id.user_item);
        }
    }
}
